package com.promis.widget;

import com.promis.widget.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;

public class ImageButton extends Button {

	private int drawableWidth;// xml文件中设置的大小
    private int drawableHeight;// xml文件中设置的大小
  
    public ImageButton(Context context) {  
        this(context, null, 0);  
    }  
  
    public ImageButton(Context context, AttributeSet attrs) {  
        this(context, attrs, 0);  
    }  
  
    public ImageButton(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;  
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageEditText);  
  
        int n = a.getIndexCount();  
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.ImageEditText_drawableWidth) {
                drawableWidth = a.getDimensionPixelSize(R.styleable.ImageEditText_drawableWidth, 50);

            } else if (attr == R.styleable.ImageEditText_drawableHeight) {
                drawableHeight = a.getDimensionPixelSize(R.styleable.ImageEditText_drawableHeight, 50);

            } else if (attr == R.styleable.ImageEditText_drawableTop) {
                drawableTop = a.getDrawable(attr);

            } else if (attr == R.styleable.ImageEditText_drawableRight) {
                drawableRight = a.getDrawable(attr);

            } else if (attr == R.styleable.ImageEditText_drawableBottom) {
                drawableBottom = a.getDrawable(attr);

            } else if (attr == R.styleable.ImageEditText_drawableLeft) {
                drawableLeft = a.getDrawable(attr);

            } else {
            }
        }  
        a.recycle();  
  
        setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);  
  
    }  
  
    /** 
     * TextView上、下、左、右设置图标 
     */  
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {  
  
        if (left != null) {  
            left.setBounds(0, 0, drawableWidth, drawableHeight);  
        }  
        if (right != null) {  
            right.setBounds(0, 0, drawableWidth, drawableHeight);  
        }  
        if (top != null) {  
            top.setBounds(0, 0, drawableWidth, drawableHeight);  
        }  
        if (bottom != null) {  
            bottom.setBounds(0, 0, drawableWidth, drawableHeight);  
        }  
        setCompoundDrawables(left, top, right, bottom);  
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int drawablePadding = getCompoundDrawablePadding();
        int minorGravity = getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK;

        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null) {
            Drawable drawableLeft = drawables[0];
            Drawable drawableRight = drawables[2];

            float textWidth = getPaint().measureText(getText().toString());
            if (drawableLeft != null) {
                int drawableWidth = drawableLeft.getIntrinsicWidth();
                float bodyWidth = textWidth + drawableWidth + drawablePadding;
                if (minorGravity != Gravity.LEFT && minorGravity != Gravity.RIGHT) {
                    setPadding(0, 0, (int) (getWidth() - bodyWidth - drawablePadding), 0);
                    canvas.translate((getWidth() - bodyWidth) / 2, 0);
                }
            } else if (drawableRight != null) {
                int drawableWidth = drawableRight.getIntrinsicWidth();
                float bodyWidth = textWidth + drawableWidth + drawablePadding;
                if (minorGravity != Gravity.LEFT && minorGravity != Gravity.RIGHT) {
                    setPadding(0, 0, (int) (getWidth() - bodyWidth - drawablePadding), 0);
                    canvas.translate((getWidth() - bodyWidth) / 2, 0);
                }
            }

        }
        super.onDraw(canvas);
    }
}
